import {Component, OnInit} from '@angular/core';
import {NzFormatEmitEvent, NzTreeNodeOptions} from 'ng-zorro-antd/tree';
import {TABS_LIST} from './constant';
import {EquipMgmtServices} from './http/service';
import {FormBuilder} from '@angular/forms';
import {HttpErrorResponse} from '@angular/common/http';

const groupArr = (list, field) => {
  let fieldList = [];
  const def = [];
  list.map((e) => {
    fieldList.push(e[field]);
  });
  fieldList = [...new Set(fieldList)];  // 数组去重

  for (let item of fieldList) {
    const arr = list.filter((l) => {
      return l.deviceDefId === item;
    });
    arr.forEach(a => {
      a.title = a.name;
      a.key = a.id;
      a.isLeaf = true;
      a.type = 'instance';
    });
    def.push({
      title: arr[0].deviceDefName,
      key: arr[0].deviceDefId,
      type: 'def',
      children: arr
    });
  }
  return def;
};


@Component({
  selector: 'app-equip-mgmt',
  templateUrl: './equip-mgmt.component.html',
  styleUrls: ['./equip-mgmt.component.less']
})
export class EquipMgmtComponent implements OnInit {
  constructor(private http: EquipMgmtServices, private fb: FormBuilder) {
  }

  nodes = [];
  currentParams = {
    instanceId: null,
    defId: null
  };
  tabsData = TABS_LIST;

  getAllLineData(): void {
    this.http.getAllLine().subscribe((response) => {
      const currentNode = [];
      for (const line of response.data) {
        currentNode.push({
          title: line.lineName,
          key: line.id,
          type: 'line'
        });
      }
      this.nodes = currentNode;
    }, () => {
    });
  }

  loadNodes(node): Promise<NzTreeNodeOptions[]> {
    return new Promise(resolve => {
      setTimeout(() => {
        if (node.origin.type === 'line') {
          this.http.getSiteByLineId(node.origin.key).subscribe(
            (response) => {
              const newSiteOptions = [];
              for (const site of response.data) {
                newSiteOptions.push({
                  title: site.siteName,
                  key: site.id,
                  type: 'site'
                });
              }
              resolve(newSiteOptions);
            }, () => {
            }
          );
        } else if (node.origin.type === 'site') {
          this.http.getInstanceBySiteId(node.origin.key).subscribe(
            (response) => {
              const newOptions = groupArr(response.data, 'deviceDefId');
              console.log(newOptions);
              resolve(newOptions);
            }, () => {
            }
          );
        }
      }, 1500);

    });
  }

  nzEvent(event: NzFormatEmitEvent): void {
    const node = event.node;
    if (event.eventName === 'expand') {

      if (node?.getChildren().length === 0 && node?.isExpanded) {
        this.loadNodes(node).then(data => {
          node.addChildren(data);
        });
      }
    } else if (event.eventName === 'click') {
      if (node.origin.type === 'instance') {
        this.currentParams.instanceId = node.origin.key;
        this.currentParams.defId = node.parentNode.origin.key;
        this.http.getInstanceAllInfo(this.currentParams.instanceId, this.currentParams.defId, 'name_plate').subscribe(
          (res) => {
            console.log(res);
          }, (error: HttpErrorResponse) => {
          }
        );
      }

    }

  }

  ngOnInit(): void {
    this.getAllLineData();
  }
}

